package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.come.Frame.ZhuFrame;
import org.come.bean.ChongjipackBean;
import org.come.bean.XXGDBean;
import org.come.entity.Goodstable;
import org.come.mouslisten.VipGoodsMouselistener;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.FormsManagement;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.UserMessUntil;
import org.come.until.Util;

import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.VipShopBtn;
import com.tool.tcpimg.UIUtils;

public class EveryDayOddsJpanel extends JPanel {

	private JLabel goodsImg;
	private String name = "VIP会员-超级圣兽丹", money = "150000仙玉";
	private VipShopBtn buyBtn;
	private VipGoodsMouselistener mouselistener;

	public EveryDayOddsJpanel() {
		setPreferredSize(new Dimension(298, 316));
		setOpaque(false);
		setLayout(null);

		// 关闭按钮事件
		FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 102);
		offBtn.setBounds(298 - 27, 2, 25, 25);
		this.add(offBtn);

		getGoodsImg();
		getBuyBtn();
	}

	/**
	 * 获取每日特惠
	 */
	public void getGoods() {
		String sendmes = Agreement.getAgreement().DayforonegetAgreement();
		SendMessageUntil.toServer(sendmes);
	}

	/**
	 * 展示
	 *
	 * @param chongjipackBeans
	 */
	public void showGoods(List<ChongjipackBean> chongjipackBeans) {

		if (chongjipackBeans == null) {
			chongjipackBeans = new ArrayList<>();
		}
		ChongjipackBean chongjipackBean = null;
		long time = Util.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String format = sdf.format(time);
		for (int i = 0; i < chongjipackBeans.size(); i++) {
			String huitime = chongjipackBeans.get(i).getHuitime();
			if (format.equals(huitime)) {
				chongjipackBean = chongjipackBeans.get(i);
				break;
			}
		}
		if (chongjipackBean == null) {
			ZhuFrame.getZhuJpanel().addPrompt2("暂无每日特惠");
			return;
			// goodsImg.setIcon(null);
			// name = null;
			// money = null;
			// mouselistener.setGoodstable(null);
			// buyBtn.setIcon(null);
			// buyBtn.setText("");
			// buyBtn.setBtn(0);
		} else {
			List<XXGDBean> goods = ChongjipackBean.getGoods(chongjipackBean.getPackgoods());
			Goodstable goodstable = UserMessUntil.getgoodstable(goods.get(0).getId());
			goodsImg.setIcon(GoodsListFromServerUntil.imgpathAdaptive(goodstable.getSkin(), 107, 107));
			name = goodstable.getGoodsname();
			money = chongjipackBean.getCanpaymoney() + "仙玉";
			mouselistener.setGoodstable(goodstable);
			buyBtn.setText("购买");
			buyBtn.setIcon(CutButtonImage.getImage("skin/8100226/button/2.png", -1, -1));
			buyBtn.setBtn(1);
		}
		if (!FormsManagement.getInternalForm(102).getFrame().isVisible()) {
			FormsManagement.showForm(102);
		}
	}

	/**
	 * 购买
	 */
	public void exchangeGoods() {
		String sendmes = Agreement.getAgreement().DayforonesureAgreement();
		SendMessageUntil.toServer(sendmes);
	}

	private ImageIcon iconBack;
	private ImageIcon iconTop;

	@Override
	protected void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);
		if (iconBack == null) {
			iconBack = CutButtonImage.getImage("inkImg/old/background/S10.png", -1, -1);
		}
		g.drawImage(iconBack.getImage(), 0, 0, 298, 316, this);

		if (iconTop == null) {
			iconTop = CutButtonImage.getImage("inkImg/old/background/S12.png", -1, -1);
		}
		g.drawImage(iconTop.getImage(), 48, 20, 230, 77, this);

		if (name != null) {
			g.setFont(UIUtils.TEXT_FONT1);
			g.drawString(name, 100, 238);
		}
		if (money != null) {
			g.setFont(UIUtils.TEXT_FONT1);
			g.setColor(Color.red);
			g.drawString(money, 100, 258);
		}
	}

	public JLabel getGoodsImg() {
		if (goodsImg == null) {
			goodsImg = new JLabel();
			goodsImg.setBounds(102, 112, 107, 107);
			mouselistener = new VipGoodsMouselistener();
			goodsImg.addMouseListener(mouselistener);
			this.add(goodsImg);
		}
		return goodsImg;
	}

	public void setGoodsImg(JLabel goodsImg) {
		this.goodsImg = goodsImg;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	public String getMoney() {
		return money;
	}

	public void setMoney(String money) {
		this.money = money;
	}

	public VipShopBtn getBuyBtn() {
		if (buyBtn == null) {
			buyBtn = new VipShopBtn("skin/8100226/button/2.png", 1, 4, "购买", this);
			buyBtn.setBounds(180, 244, 34, 17);
			this.add(buyBtn);
		}
		return buyBtn;
	}

	public void setBuyBtn(VipShopBtn buyBtn) {
		this.buyBtn = buyBtn;
	}

	public ImageIcon getIconBack() {
		return iconBack;
	}

	public void setIconBack(ImageIcon iconBack) {
		this.iconBack = iconBack;
	}

	public ImageIcon getIconTop() {
		return iconTop;
	}

	public void setIconTop(ImageIcon iconTop) {
		this.iconTop = iconTop;
	}

	public VipGoodsMouselistener getMouselistener() {
		return mouselistener;
	}

	public void setMouselistener(VipGoodsMouselistener mouselistener) {
		this.mouselistener = mouselistener;
	}

}
